<?php
/**
 * Created by PhpStorm.
 * User: Monkey
 * Date: 2021/10/13
 * Time: 11:27
 * Desc: 日息宝
 */
declare (strict_types=1);

namespace app\backend\controller;

use app\backend\base\controller\Base;
use app\backend\validate\RixibaoValidate;

class Rixibao extends Base
{
    /**
     * 列表
     */
    public function index()
    {
        if(request()->isPost()){
            $validate = new RixibaoValidate();
            $ret = $validate->scene('index')->check(input());
            if(!$ret){
                return json(_E($validate->getError()));
            }

            $page = input('page/d',1);
            $limit = input('limit/d',20);
            $type = input('type/d', '');
            $fieldName = input('field_name','');
            $fieldValue = input('field_value','');
            $dateType = input('date_type','created_at');
            $startDate = input('start_date','');
            $endDate = input('end_date','');

            $where = [
                [$dateType,'between',[$startDate,$endDate]]
            ];

            //字段筛选
            if (!empty($fieldName) && !empty($fieldValue)) {
                switch ($fieldName) {
                    case 'account'://账号
                        $userId = \app\extend\model\User::where([
                            ['account','=',$fieldValue]
                        ])->value('id');
                        $where[] = ['uid', '=', $userId];
                        break;
                    case 'uid'://用户ID
                        $where[] = ['uid', '=', $fieldValue];
                        break;
                }
            }

            //类型
            if(is_numeric($type)){
                $where[] = ['type', '=', $type];
            }

            $list = [];
            $count = \app\extend\model\Rixibao::where($where)->count();
            if($count > 0){
                $list = \app\extend\model\Rixibao::where($where)->order(['id'=>'desc'])->page($page)->limit($limit)->select();

                foreach ($list as $value){
                    $userInfo = \app\extend\model\User::where([
                        ['id','=',$value->uid]
                    ])->append(['level_name_txt'])->field('id,level_id,account')->find();

                    $value->account = $userInfo->account;
                    $value->level_name = $userInfo->level_name_txt;
                }
            }

            return json([
                'code' => 0,
                'msg' => '获取成功',
                'count' => $count,
                'data' => $list,
            ])->send();
        } else {
            return view('index');
        }
    }
}
